package com.bjsxt.userscenter.common.util;

import javax.annotation.Resource;
import javax.mail.internet.MimeMessage;

import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

@Component("springEmailUtil")
public class SpringEmailUtil 
{
	@Resource(name="javaMailSenderImpl")
	private JavaMailSender javaMailSender;
	@Resource
	private SimpleMailMessage simpleMailMessage;
	
	public boolean sendMail(String to,String subject,String text){
		try{
			this.simpleMailMessage.setTo(to);
			this.simpleMailMessage.setSubject(subject);
			this.simpleMailMessage.setText(text);
			this.javaMailSender.send(this.simpleMailMessage);
			return true;
		}catch(Exception e){
			ConstatFinalUtil.LOGGER.error("---发送邮件失败了---to:{},subject:{},text:{}--",to,subject,text,e);
			
		}
		return false;
	}
	public boolean sendMailHtml(String to,String subject,String text){
		try{
			MimeMessage message=this.javaMailSender.createMimeMessage();
			MimeMessageHelper helper=new MimeMessageHelper(message,true,"UTF-8");
			helper.setFrom(this.simpleMailMessage.getFrom());
			helper.setTo(to);
			helper.setSubject(subject);
			helper.setText(text,true);
			this.javaMailSender.send(message);
			return true;
		}catch(Exception e){
			ConstatFinalUtil.LOGGER.error("--发送邮件html失败了------to:{},subject:{},text:{}------",to,subject,text,e);
		}
		return false;
	}
	
}
